jQuery(function ($) {
  $('#page_data_table').on('focusout', 'tbody tr td.column_priority input', function () {
    if (!pageDataTable) { return; }
    var jThis = $(this);
    var newPriority = jThis.val();
    newPriority = newPriority && parseInt(newPriority) || 0;
    var jTr = $(this).closest('tr');
    var rowData = pageDataTable.row(jTr).data();
    var oldPriority = rowData.priority && parseInt(rowData.priority) || 0;
    if (newPriority === oldPriority) {
      return;
    }
    var id = rowData.id && parseInt(rowData.id) || 0;
    if (id < 1) {
      return;
    }
    var postData = { _op_: 'updatePriority', id: id, priority: newPriority };
    $.ajax({
      method: 'POST',
      url: window.location.pathname,
      contentType: 'application/json',
      data: JSON.stringify(postData),
      dataType: 'json',
      success: function (data, textStatus, jqXHR) {
        // console.log(data);
        if (data && data.code && parseInt(data.code) < 400) {
          pageDataTable.draw('page');
        } else {
          alert(data && data.msg || '服务器错误，请稍后重试!');
          jThis.val(oldPriority);
        }
      },
      error: function (jqXHR, textStatus, errorThrown) {
        alert('网络错误，请稍后重试!');
      }
    });
  });
});